package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryAchievementCategoryByEntityReq;
import com.sg.dto.base.app.req.QueryAchievementCategoryDetailReq;
import com.sg.dto.base.app.req.QueryAchievementCategoryListReq;
import com.sg.entity.OmsAchievementCategory;
import com.sg.service.base.app.MOmsAchievementCategoryService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementCategoryMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementCategoryServiceImpl
        extends ServiceImpl<OmsAchievementCategoryMapper, OmsAchievementCategory>
        implements MOmsAchievementCategoryService {

    @Autowired
    OmsAchievementCategoryMapper omsAchievementCategoryMapper;

    /**
     * 3-10-03新增成果类别[9103]
     * gen by moon at 4/3/2024, 1:43:09 PM
     */
    @Trace(operationName = "3-10-03新增成果类别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementCategory(OmsAchievementCategory omsAchievementCategory) {
        omsAchievementCategory.setAchievementCategoryId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementCategory.setIsArchive("FALSE");
        omsAchievementCategory.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementCategory.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementCategory != null && omsAchievementCategory.getSubjectLifeCycle() != null && omsAchievementCategory.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCategory.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementCategory.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementCategory.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementCategory != null && omsAchievementCategory.getSubjectLifeCycle() != null && omsAchievementCategory.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCategory.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementCategory.setIsValid("FALSE");
        if (super.save(omsAchievementCategory)) {
            return omsAchievementCategory.getAchievementCategoryId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-03修改成果类别[9105]
     * gen by moon at 4/3/2024, 1:43:09 PM
     */
    @Trace(operationName = "3-10-03修改成果类别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementCategory(OmsAchievementCategory omsAchievementCategory) {
        omsAchievementCategory.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementCategory != null && omsAchievementCategory.getSubjectLifeCycle() != null && omsAchievementCategory.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCategory.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementCategory != null && omsAchievementCategory.getSubjectLifeCycle() != null && omsAchievementCategory.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementCategory.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievementCategory.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementCategory != null && omsAchievementCategory.getSubjectLifeCycle() != null && omsAchievementCategory.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCategory.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementCategory != null && omsAchievementCategory.getSubjectLifeCycle() != null && omsAchievementCategory.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementCategory.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementCategory);
    }

    /**
     * 3-10-03删除成果类别[9106]
     * gen by moon at 4/3/2024, 1:43:10 PM
     */
    @Trace(operationName = "3-10-03删除成果类别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementCategory(String achievementCategoryId) {

        return super.removeById(achievementCategoryId);
    }

    /**
     * 3-10-03查询成果类别列表[9107]
     * gen by moon at 4/3/2024, 6:42:42 PM
     */
    @Trace(operationName = "3-10-03查询成果类别列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementCategory> queryAchievementCategoryList(QueryAchievementCategoryListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementCategory> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAchievementCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-03查询成果类别详情[9108]
     * gen by moon at 4/3/2024, 1:43:17 PM
     */
    @Trace(operationName = "3-10-03查询成果类别详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievementCategory queryAchievementCategoryDetail(QueryAchievementCategoryDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAchievementCategory> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievementCategory> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievementCategory retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-03批量查询成果类别By主键[9268]
     * gen by moon at 4/5/2024, 8:58:32 PM
     */
    @Trace(operationName = "3-10-03批量查询成果类别By主键")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementCategory> batchQueryAchievementCategoryByEntity(BatchQueryAchievementCategoryByEntityReq reqDto) {

        try {
            QueryWrapper<OmsAchievementCategory> queryWrapper = new QueryWrapper<>();

            if (reqDto.getAchievementCategoryList() == null || reqDto.getAchievementCategoryList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("achievement_category_id", reqDto.getAchievementCategoryList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
